// pages/purchaser-apply/purchaser-apply.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList:[],
    inputTip:'请输入金额'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { mealTime, canteenId, supplierId, batchOrderId} = JSON.parse(options.data)
    this.setData({ mealTime, canteenId, supplierId, batchOrderId})

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  priceChnage(e){

    this.setData({ price: e.detail.value})
  },

  afterRead(event) {
 
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    file.map(item => {
      wx.uploadFile({
        url: 'https://api.canteen.jiangshan.hzzhixun.cn/canteen-api/canteens/oss/upload',
        filePath: item.path,
        name: 'file',
        formData: { 'user': 'test' },
        success: res => {
          // 上传完成需要更新 fileList
          const { fileList } = this.data;
          fileList.push({ ...file, url: JSON.parse(res.data).data.src });
          console.log(fileList)
          this.setData({ fileList });
        }
      });
    })
  },
  delImg(e) {
    let { fileList } = this.data
    fileList.splice(e.detail.index, 1)
    this.setData({ fileList })
  },
  handelConfirm(){
    const { fileList, mealTime, price, canteenId, supplierId, batchOrderId}=this.data


    console.log(batchOrderId)
    let photo = []
    fileList.map(item => {
      photo.push(item.url)
    })

    if (!photo.join(',') || !price) {
      wx.showToast({
        title: '请填写价格和图片',
        icon: 'none'
      })
      return
    }
    app.reqFetch.batchorderupdatePhoto({
      batchOrderId,
      entityPhoto: photo.join(','),
    }, (res) => {


    })
    app.reqFetch.getImportAmount({
      createDate: mealTime,
      entityPhoto: photo.join(','),
      realityMoney: price,
      canteenId,
      batchOrderId
    }, (res) => {
      const { money}=res
      if (Math.abs(money) === parseInt(price)){
        wx.navigateTo({
          url: '/pages/purchaser-details/purchaser-details?data=' + JSON.stringify({ price, supplierId, audit: true, canteenId, batchOrderId}),
        })
      }else{
        this.setData({ inputTip: '金额有误，请重新输入' })
        this.setData({ price:''})
      }
    })
  
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})